package org.apache.click.doubleclick.injectspring;

import org.apache.click.control.AbstractControl;
import org.apache.click.doubleclick.inject.DependencyInjectorPageInterceptor;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;

/**
 * Responsible for loading spring context. By default it will look for <i>applicationContext.xml</i>
 * into <i>WEB-INF</i> directory. To override the default location we have to declare
 * <i>contextConfigLocation</i> context param on <i>web.xml</i>. Ex-
 * <p/>
 * <pre>
 * {@code
 * <context-param>
 *       <param-name>contextConfigLocation</param-name>
 *       <param-value>/WEB-INF/myApplicationContext.xml</param-value>
 * </context-param>
 * }
 * </pre>
 *
 * @Author Jahid Shohel(development.jsh@gmail.com)
 */
public class SpringControl extends AbstractControl {
    @Override
    public void onDeploy(ServletContext servletContext) {
        ContextLoader contextLoader = new ContextLoader();
        WebApplicationContext applicationContext = contextLoader.initWebApplicationContext(servletContext);
        servletContext.setAttribute(DependencyInjectorPageInterceptor.DEPENDENCY_INJECTOR, new SpringDependencyInjector());
        servletContext.setAttribute("dependencyInjectorContext", applicationContext);
    }
}
